package com.lnsoft.cac.server.handle.codec.frameMessage;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

@Data
/**
 * 配置/状态交互---监测仪时间
 * */
public class Cfg_Time_Msg {
    private int Request_Set_Flag;       //1byte     参数配置类型标识：①0x00查询配置信息②0x01设置配置信息
    private int Clocktime_Stamp;        //4byte     设置时间（采用世纪秒表示）

    public ByteBuf toByteBuf() throws Exception{
        if (Request_Set_Flag != (byte)0x00 && Request_Set_Flag != (byte)0x01) {
            throw new Exception("数据发送状态 设置异常");
        }
        if (!Util.statTimeIsAvailable(Clocktime_Stamp)) {
            throw new Exception("当前时间 设置异常");
        }
        ByteBuf bf = Unpooled.buffer(5);
        bf.writeByte(Request_Set_Flag);
        bf.writeIntLE(Clocktime_Stamp);
        return bf;
    }


    //  解析 { 配置/状态交互---监测仪时间} 帧报文内容区 到消息体中
    public static Cfg_Time_Msg decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        Cfg_Time_Msg msg = new Cfg_Time_Msg();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        if (len != 5) {
            throw new Exception("frameBody 异常");
        }
        msg.setRequest_Set_Flag(bf.getByte(0) & 0xFF);
        msg.setClocktime_Stamp(bf.getIntLE(1) & 0xFFFFFFFF);
        return msg;
    }

}
